<template>
	<sc-dialog v-model="visible" :title="titleMap[mode]" show-fullscreen destroy-on-close width="750px" @close="close">
		<el-form ref="formRef" label-width="140px" :model="formData" :rules="rules">
			<el-form-item label="工艺路线" prop="routingId">
				<el-input v-model="formData.routingName" disabled />
			</el-form-item>
			<el-form-item label="产品" prop="productId">
				<sc-table-select
					v-model="prodcutSelectedValue"
					placeholder="请选择产品"
					:apiObj="apiObj"
					:params="reqParams"
					:props="{
						label: 'itemName',
						value: 'id',
					}"
					@change="change">
					<el-table-column prop="itemCode" label="产品编号" width="120" />
					<el-table-column prop="itemName" label="产品名称" width="220" />
				</sc-table-select>
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">确 定</el-button>
		</template>
	</sc-dialog>
</template>

<script>
export default {
	emits: ['complete'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			isSaveing: false,
			visible: false,
			apiObj: this.$API.procroutingproduct.pagesproducts,
			prodcutSelectedValue: {},
			reqParams: {
				routingId: 0,
				includeProductId: 0,
			},
			formData: {
				id: 0,
				productId: undefined,
				routingId: undefined,
				routingName: '',
			},
			rules: {
				routingId: [{ required: true, message: "请选择工艺路线", trigger: "blur" }],
				productId: [{ required: true, message: "请选择产品", trigger: "blur" }],
			},
		};
	},
	methods: {
		change(item) {
			this.formData.productId = item.id;
		},
		async open(row, param) {
			this.reqParams.routingId = param.routingId;
			this.reqParams.includeProductId = 0;

			if (!row) {
				this.mode = "add";
				Object.assign(this.formData, param);
			} else {
				this.mode = "edit";

				const resp = await this.$API.procroutingproduct.model.get(row.id);
				Object.assign(this.formData, resp.data, param);
				this.prodcutSelectedValue = { id: this.formData.productId, itemName: row.product.itemName };
				this.reqParams.includeProductId = this.formData.productId;
			}

			this.visible = true;
		},
		save() {
			this.$refs.formRef.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let resp = null;
					if (this.formData.id === 0) {
						resp = await this.$API.procroutingproduct.add.post(this.formData);
					} else {
						resp = await this.$API.procroutingproduct.update.put(this.formData);
					}
					this.isSaveing = false;
					if (resp.code == 200) {
						this.$emit("complete");
						this.visible = false;
						this.$message.success("保存成功");
					} else {
						this.$alert(resp.message, "提示", { type: "error" });
					}
				}
			});
		},
		close() {
			this.prodcutSelectedValue = {};
			this.formData = {
				id: 0,
				productId: undefined,
				routingId: undefined,
				routingName: '',
			};
			this.$refs.formRef.resetFields();
			this.visible = false;
		},
	},
};
</script>
